﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Burn4You;
using MediaPortal.GUI.Library;
using MediaPortal.Dialogs;

namespace VideoDisk
{
    public enum VideoDiskScreen
    {
        DiskSelection,
        FileSelection,
        Menus,
        Summary
    }

    public class VideoDiskExt : Extension, IExtension
    {
        #region GUI Controls

        // Visiblity toggle controls. Changing the visible property on these controls toggles the visibility of all the controls on that screen.
        [SkinControlAttribute(1230)]        protected GUILabelControl dummySummary = null;

        // Global controls
        [SkinControlAttribute(1)]        protected GUIButtonControl btnBack = null;
        [SkinControlAttribute(2)]        protected GUIButtonControl btnNext = null;

        // Disk Type Selection screen controls


        #endregion

        #region Variables

        private String name = "Video Disk Extension";
        private String image = "";
        private VideoDiskScreen CurrentScreen = VideoDiskScreen.DiskSelection;
        private DiskType disktype;
        private DiskTemplate template;

        private Dictionary<int, BurnFile> FilesToBurn;

        #endregion

        #region IExtension Members
        
        public string ExtName
        {
            get { return name; }
        }

        public string Image
        {
            get { return image; }
        }

        public bool HasFiles
        {
            get { return false; }
        }

        public bool HasUI
        {
            get { return true; }
        }

        public List<String> SupportedBurnTypes()
        {
            List<String> SBT = new List<String>(){"Video", "TV Show", "Movie"};
            
            return SBT;
        }

        #endregion

        #region Extension overrides



        #endregion

        #region GUIWindow overrides
        
        public override bool Init()
        {
            FilesToBurn = new Dictionary<int, BurnFile>();

            return Load(GUIGraphicsContext.Skin + @"\Burn4YouVideoDiskExt.xml");
        }

        protected override void OnPageLoad()
        {
            base.OnPageLoad(); // this must be first

            Utils.Burn4YouLog.Write("Video Disk Extension: OnPage Load called.");

            if (CurrentScreen == VideoDiskScreen.Menus && TemplateSelectionScreen.CallingWindow == GetID)
            {
                Utils.Burn4YouLog.Write("Video Disk Extension: Returning from Template Selection Screen.");

                if (TemplateSelectionScreen.Completed)
                {
                    Utils.Burn4YouLog.Write("Video Disk Extension: Received template: " + TemplateSelectionScreen.ChosenTemplate.Name);

                    template = TemplateSelectionScreen.ChosenTemplate;

                    CurrentScreen = VideoDiskScreen.Summary;
                }
                else
                {
                    ShowFileSelectionScreen();
                    return;
                }
            }

            if (CurrentScreen == VideoDiskScreen.FileSelection && FilesSelectionScreen.CallingWindow == GetID)
            {
                Utils.Burn4YouLog.Write("Video Disk Extension: Returning from File Selection Screen.");

                if (FilesSelectionScreen.Completed)
                {
                    Utils.Burn4YouLog.Write("Video Disk Extension: Received " + FilesSelectionScreen.ChosenFiles.Count.ToString() + " Files");

                    foreach (BurnFile file in FilesSelectionScreen.ChosenFiles)
                        FilesToBurn.Add(FilesToBurn.Count + 1, file);

                    ShowTemplateSelectionScreen();
                }
                else
                {
                    ShowDiskTypeSelectionScreen();
                    return;
                }

            }

            if (CurrentScreen == VideoDiskScreen.DiskSelection && DiskTypeSelectionScreen.CallingWindow == GetID)
            {
                Utils.Burn4YouLog.Write("Video Disk Extension: Returning from Disk Type Selection Screen.");

                if (DiskTypeSelectionScreen.Completed)
                {
                    disktype = DiskTypeSelectionScreen.ChosenType;
                    ShowFileSelectionScreen();
                    return;
                }
                else
                    GUIWindowManager.CloseCurrentWindow();
            }

            if (CurrentScreen == VideoDiskScreen.DiskSelection)
            {
                ShowDiskTypeSelectionScreen();
                return;
            }

            
            

            LoadScreen(CurrentScreen);
        }

        protected override void OnClicked(int controlId, GUIControl control, MediaPortal.GUI.Library.Action.ActionType actionType)
        {
            if (control == btnNext)
            {
                switch(CurrentScreen)
                {
                    case VideoDiskScreen.Summary:
                        StartBurnProcess();
                        break;
                }
            }
            else if (control == btnBack)
            {
                switch(CurrentScreen)
                {
                    case VideoDiskScreen.Summary:
                        ShowTemplateSelectionScreen();
                        break;
                }
            }


            base.OnClicked(controlId, control, actionType);
        }

        public override int GetID
        {
            get
            {
                return 54270;
            }
        }

        #endregion

        public VideoDiskExt()
        {
            Utils.Burn4YouLog.Write("Video Disk Extension: Creating Video Disk Extension class");
        }

        #region Functions

        private void StartBurnProcess()
        {
            Utils.Burn4YouLog.Write("Video Disk Extension: Starting Burn Process.");
            EncoderHelper me = Burn4YouPlugin.MediaEncoderHelper;
            me.AllFilesFinishedEvent += new EncoderHelper.AllFilesFinishedHandler(me_AllFilesFinished);
            me.ShowGUI = true;

            List<EncodeSettings> FilesToEncode = new List<EncodeSettings>();

            foreach (KeyValuePair<int, BurnFile> file in FilesToBurn)
            {
                Utils.Burn4YouLog.Write("Video Disk Extension: Attempting to encode file \"" + file.Value.FilePath + "\"");
                // encode the files
                

                EncodeSettings es = new EncodeSettings();
                
                switch(disktype)
                {
                    case DiskType.DVD:
                        es.Target = "dvd";
                        break;

                    case DiskType.VCD:
                        es.Target = "vcd";
                        break;

                    case DiskType.SVCD:
                        es.Target = "svcd";
                        break;

                    case DiskType.BluRay:
                        es.Target = "bluray";
                        break;
                }

                if (Burn4YouPlugin.settings.DefaultFormat == "PAL")
                    es.Target += "-pal";
                else
                    es.Target += "-ntsc";
                
                es.OutputFileName = file.Value.Name + ".mpeg";
                es.MediaPath = file.Value.FilePath;

                FilesToEncode.Add(es);
            }

            me.EncodeFiles(FilesToEncode);
        }

        void me_AllFilesFinished(List<EncoderResult> results)
        {

            Utils.Burn4YouLog.Write("Video Disk Extension: All files finished encoding.");
        }

        private void BuildDiskStructure()
        {


        }

        private void ShowControls()
        {
            dummySummary.Visible = false;
            

            switch (CurrentScreen)
            {
                case VideoDiskScreen.Summary:
                    
                    dummySummary.Visible = true;

                    btnNext.Focus = true;

                    break;
                    
            }

        }

        private void LoadScreen(VideoDiskScreen screen)
        {
            CurrentScreen = screen;
            
            switch (screen)
            {
                case VideoDiskScreen.Summary:
                    btnNext.Label = "Burn";
                    Utils.setGUIProperty("#Burn4YouVideoDisk.FileCount", FilesToBurn.Count.ToString());
                    break;
            }
            
            Utils.Burn4YouLog.Write("Video Disk Extension: Screen \"" + CurrentScreen.ToString() + "\" loaded");

            ShowControls();
        }

        private void ShowFileSelectionScreen()
        {
            FilesSelectionScreen.diskType = disktype;
            FilesSelectionScreen.FileTypesToGet = "Movie,TV Show,Video";

            CurrentScreen = VideoDiskScreen.FileSelection;

            GUIWindowManager.ActivateWindow((int)Screen.FileSelection);
        }

        private void ShowDiskTypeSelectionScreen()
        {
            DiskTypeSelectionScreen.DiskTypesToShow = new List<DiskType>() { DiskType.VCD, DiskType.SVCD, DiskType.DVD, DiskType.BluRay };

            CurrentScreen = VideoDiskScreen.DiskSelection;

            GUIWindowManager.ActivateWindow((int)Screen.DiskType);
        }

        private void ShowTemplateSelectionScreen()
        {
            CurrentScreen = VideoDiskScreen.Menus;

            GUIWindowManager.ActivateWindow((int)Screen.TemplateSelection);
        }

        #endregion

    }
}
